import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class SearchResultsDialog extends JFrame {
	
	private JLabel jLabel1;
	private JLabel rateLabel;
	private JLabel sortByLabel;
	private JLabel saveLabel;
    private static SearchResultsDialog dialog;
    private static String value = "";
    private JList list;
    private JButton rateButton;
    private JButton savePrefsButton;
    private JButton sortByButton;
    private JPanel row;
    private JPanel rateCol;
    private JPanel sortByCol;
    private JPanel savePrefsCol;
    private JComboBox rateList;
    private JComboBox sortList;
    private JTextField savePrefsTextField;
    private DomainLogic dL; 
    private String zipCode;
    private String foodType;
    private Preferences prefs;
    private Vector<Restaurant> restV = new Vector<Restaurant>();
    JScrollPane listScroller;

	
    public SearchResultsDialog(){
    	
    }
    
    
    public SearchResultsDialog(Vector<Restaurant> restaurantVector, 
    							DomainLogic _dL, 
    							Boolean userLogged, 
    							Boolean adminLogged, 
    							String _foodType,
    							String _zipCode,
    							Preferences _prefs,
    							String savedProfile){

    	dL= _dL;
    	zipCode = _zipCode;
    	foodType = _foodType;
    	prefs = _prefs;
    	restV = restaurantVector;
    	createTextArea(restaurantVector);
   	

    	Border blackline = BorderFactory.createLineBorder(Color.black);
    	// create option buttons
    	row = new JPanel();
    	rateCol = new JPanel();
    	sortByCol = new JPanel();
    	savePrefsCol = new JPanel();
    	row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
    	savePrefsCol.setLayout(new BoxLayout(savePrefsCol, BoxLayout.Y_AXIS));
    	rateCol.setLayout(new BoxLayout(rateCol, BoxLayout.Y_AXIS));
    	sortByCol.setLayout(new BoxLayout(sortByCol, BoxLayout.Y_AXIS));
    	
    	//if user is logged create save prefs and rate options
    	if(userLogged || adminLogged){
    		if(savedProfile != null){
    			JLabel profileName = new JLabel("Profile name: "+savedProfile);
    			JLabel foodName = new JLabel("Type of Food: "+ foodType);
    			savePrefsCol.add(profileName);
    			savePrefsCol.add(foodName);
    			this.add(savePrefsCol);
    		}
    		else{
    			createSavePrefsOption(blackline);
    		}
    		createRateOption(blackline);
    	}
		createSortOption(blackline,restaurantVector);
		
		this.add(row);
	}
    
    public void createTextArea(Vector<Restaurant> restaurantVector){
		String[] results = new String[restaurantVector.size()];
		for(int i = 0; i < restaurantVector.size(); i++)
			results[i] = restaurantVector.get(i).getName()+ " " + restaurantVector.get(i).getAddress() + "\t  |  \t " + restaurantVector.get(i).getPreferences().getCost().get(0) + " \t  |  \t " + restaurantVector.get(i).getRating(); //add address
		
		this.setLayout(new GridLayout(0,1));
		//this.setMinimumSize(new Dimension(400,400));
		
		list = new JList(results);
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		list.setVisibleRowCount(-1);
		listScroller = new JScrollPane(list);
		listScroller.setPreferredSize(new Dimension(250, 80));
		jLabel1 = new JLabel("Search Results\n Name      \t\t     Cost     \t\t      Average Rating");
		this.add(jLabel1);
		this.add(listScroller);			
		this.setSize(400,200);
    }
    
    public void createSavePrefsOption(Border blackline){
    	saveLabel = new JLabel("Save results/preferences to a profile:");
		savePrefsTextField = new JTextField();
		savePrefsButton = new JButton("Save");
		savePrefsCol.add(saveLabel);
		savePrefsCol.add(savePrefsTextField);
		savePrefsCol.add(savePrefsButton);
		savePrefsCol.setBorder(blackline);
		savePrefsButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
            {
        		if(savePrefsTextField.getText().isEmpty()){
        			new ErrorMessageDialog("Profile name was not specified");
        		}
        		else{
        			UserPreferences userPrefs = new UserPreferences(savePrefsTextField.getText(), zipCode, foodType,prefs);
        			dL.addUserPreferences(userPrefs);
        		}
            }
		});
		//savePrefsButton.setMaximumSize(new Dimension(60,30));
		row.add(savePrefsCol);
    	
    }
    
    public void createRateOption(Border blackline){
		rateLabel = new JLabel("Rate Restaurant:");
		rateCol.add(rateLabel);
		String[] rateStrings = { "5","4", "3", "2", "1"};
		rateList = new JComboBox(rateStrings);
		rateList.setSelectedIndex(0);
		rateList.setMaximumSize(new Dimension(200,20));
		rateButton = new JButton("Rate");
		rateCol.add(rateList);
		rateCol.add(rateButton);
		//sortPrefsButton.setMaximumSize(new Dimension(60,30));
		rateCol.setBorder(blackline);
		row.add(rateCol);

    }
    
    public void createSortOption(Border blackline, Vector<Restaurant> restaurantVector){
		sortByLabel = new JLabel("Sort Results By");
		sortByCol.add(sortByLabel);
		String[] sortStrings = { "Rating (ascending)", "Rating (descending)",
								 "Cost   (ascending)", "Cost   (descending)"};
		sortList = new JComboBox(sortStrings);
		sortList.setSelectedIndex(0);
		sortList.setMaximumSize(new Dimension(200,20));
		sortByButton = new JButton("Sort");
		sortByCol.add(sortList);
		sortByCol.add(sortByButton);
		sortByCol.setBorder(blackline);
		//savePrefsButton.setMaximumSize(new Dimension(60,30));
		restV = restaurantVector;
/*		sortByButton.addActionListener(new ActionListener(){
	        public void actionPerformed(ActionEvent e)
	            {
	
   
        			createTextArea(restV);
        			//listScroller.setVisible(false);
/*        			list = new JList(restV);
        			list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        			list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        			list.setVisibleRowCount(-1);
        			listScroller = new JScrollPane(list);
        			listScroller.setPreferredSize(new Dimension(250, 80));
        			listScroller.setVisible(true);
  
	            }	        
			});	
	*/	
		row.add(sortByCol);
    }
    
	public JButton getSortButton(){
		return sortByButton;
	}
    
	
	public JComboBox getSortList(){
		return sortList;
	}
	
	public JButton getRateButton(){
		return rateButton;
	}	
	
	public String getRestName(){
		return restV.get(list.getSelectedIndex()).getName();
	}
	
	public JComboBox getRateList(){
		return rateList;
	}
}
